<?php 
/**
 * 投保记录model类
 * @Author    紫梦
 * @DateTime  2017-11-14
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\common\model;
use think\Model;
use think\Db;
class Secure extends Model
{
	protected $autoWriteTimestamp = true;
	protected $type = [
        'secure_start'  =>  'timestamp:Y-m-d',        
        'secure_end'  =>  'timestamp:Y-m-d', 
             
    ];
    protected $insert = ['agency_id'];
    
    protected function setAgencyIdAttr()
    {
        return session('admin.agency_id');
    }    

	// 定义全局的查询范围
    /*protected static function base($query)
    {
    	$agency = session('admin.agency_id');
        if($agency==1){
            // $query->where('delete_time',null);

        }else{
            $query->where(['agency_id'=>$agency]);

        }
    		    	
    }
*/
    public function secureFile()
    {
        return $this->hasMany('SecureFile','secure_id');
    }

    /*public function carsInfo()
    {
        return $this->hasOne('Cars','car_id','car_id')->field('car_sn,car_unique_id');
    }*/

    public function getCarIdAttr($value){
        return Db::name('cars')->where(['car_id'=>$value])->field('car_sn,car_unique_id')->find();
    }

    public function scopeSecureNum($query,$time)
    {
        $query->where('secure_end','<',$time);
    }

    public function agency()
    {
        return $this->hasOne('Agency','id','agency_id')->field('id,name');
    }
}